package com.zhouxiao.multithreading.线程执行顺序;

/**
 * @author 周小哥  gqzmy@163.com 2023/3/3 21:48
 * <p>
 * 需求：
 * 1、1号线程、2号线程、3号线程都要执行
 * 2、执行是有顺序的，1->2->3
 * <p>
 * 实现：使用线程的join方法，使线程顺序执行
 */

public class ThreadJoinDemo {

    public static void main(String[] args) {

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("1号线程");
            }
        });


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    thread1.join();
                    System.out.println("2号线程");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
            }
        });

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    thread2.join();
                    System.out.println("3号线程");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
            }
        });
        System.out.println("开始。。。");
        thread2.start();
        thread1.start();
        thread3.start();

    }
}
